package net.cctms.zhishu.webAppFramework.utils;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import net.cctms.zhishu.R;


public class ImgUtils {

    //是否设置Webp格式
    public static boolean webpSwitch;
    public static final int IMG_SIZE_BOUNDARY = 720;
    public static final int SMALL_IMG_MAX_WIDTH = 460;
    public static final int BIG_IMG_MAX_WIDTH = 640;

    static {
        webpSwitch = DefaultShared.getBoolean(Constants.KEY_WEBP_IMAGE_SWITCH, false);
    }

    /**
     * 设置Webp格式的开关
     *
     * @param enable
     */
    public static void setWebpSwitch(boolean enable) {
        webpSwitch = enable;
        DefaultShared.getBoolean(Constants.KEY_WEBP_IMAGE_SWITCH, webpSwitch);
    }

    /**
     * 转换为webp格式图片
     *
     * @param imgUrl
     * @return
     */
    public static String convertWebpSuffix(String imgUrl) {
        if (webpSwitch ) {
            int lastPointIndex = imgUrl.lastIndexOf(".");
            if (lastPointIndex <= 0) {
                return imgUrl;
            }
            String suffixStr = imgUrl.substring(lastPointIndex + 1);
            if (suffixStr.equalsIgnoreCase("png")
                    || suffixStr.equalsIgnoreCase("jpg")
                    || suffixStr.equalsIgnoreCase("jpeg")) {
                imgUrl = imgUrl.substring(0, lastPointIndex) + ".webp";
            }

            //imgUrl = imgUrl.replaceFirst("http://","https://");

        }
        return imgUrl;
    }


    /**
     * 转换为通知栏缩略图尺寸
     */
    public static String getNotificationThumbnailImageUrl(Context context, String originUrl) {
        if (TextUtils.isEmpty(originUrl)) {
            return "";
        }
        if (context == null) {
            return "";
        }
        int picDimens = context.getResources().getDimensionPixelOffset(R.dimen.notification_icon_size);
        return originUrl.replace("/o/", "/s" + picDimens + "x" + picDimens + "/");
    }

    /**
     * 获取图片缩放之后的高度
     * 1.手机屏幕宽度大于IMG_SIZE_BOUNDARY
     * 如果图片宽度大于BIG_IMG_MAX_WIDTH则用BIG_IMG_MAX_WIDTH,图片高度根据比例缩放，并且url以m的形式进行变换；如果小于则用原图
     * 2.手机屏幕宽度小于IMG_SIZE_BOUNDARY
     * 如果图片宽度大于SMALL_IMG_MAX_WIDTH则用SMALL_IMG_MAX_WIDTH,图片高度根据比例缩放，并且url以m的形式进行变换；如果小于则用原图
     *
     * @param imgWidth    图片宽度
     * @param imgHeight   图片高度ca
     * @param screenWidth 手机屏幕宽度
     * @return scaleSize[0]图片宽度 scaleSize[1]图片高度 scaleSize[2]是否需要以及M形式裁剪图片 0为不需要，1为需要
     */
    public static int[] getScaleImgSize(int imgWidth, int imgHeight, int screenWidth) {
        int[] scaleSize = new int[3];
        float imgRatio;

        scaleSize[0] = imgWidth;
        scaleSize[1] = imgHeight;
        scaleSize[2] = 0;

        //如果图片宽高非法则不对其进行转换
        if (imgWidth != 0 && imgHeight != 0) {
            if (screenWidth > IMG_SIZE_BOUNDARY) {
                if (imgWidth > BIG_IMG_MAX_WIDTH) {
                    scaleSize[0] = BIG_IMG_MAX_WIDTH;
                    imgRatio = (float) BIG_IMG_MAX_WIDTH / (float) imgWidth;
                    scaleSize[1] = (int) ((float) imgHeight * imgRatio);
                    scaleSize[2] = 1;
                }
            } else {
                if (imgWidth > SMALL_IMG_MAX_WIDTH) {
                    scaleSize[0] = SMALL_IMG_MAX_WIDTH;
                    imgRatio = (float) SMALL_IMG_MAX_WIDTH / (float) imgWidth;
                    scaleSize[1] = (int) ((float) imgHeight * imgRatio);
                    scaleSize[2] = 1;
                }
            }
        }

        return scaleSize;
    }

}

